/**
 * Created by zycool on 2016/11/11.
 * 内检数据对齐 - 完整性分析
 */


;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点
    var $begin = $('#JQ_begin');
    // 终点
    var $end = $('#JQ_end');
    // 内检测项目名称
    var $inside_task = $('#JQ_inside_task').select3({
        search: false
    });
    // 外检时间 - 开始
    var $start_date = $('#JQ_start_date');
    // 外检时间 - 结束
    var $end_date = $('#JQ_end_date');
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
            // 内检测项目名称
            DyyBase.ajax(analysisApi.url_inside_task(), {pipeLineId: id}, function (dataset) {
                if (dataset.bool) {
                    var json = DyyBase.getSelect(dataset.result);
                    $inside_task.add(json);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
            $segment.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
        }
    });

    // 查询对象
    var filter = {
        getSearch: function () {
            var data = {};
            data.mode = true;
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({
                        content: '管道名称不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                } else {
                    data.mode = false;
                }
                return '';
            }();
            data.startMileage = function () {
                var v = $.trim($begin.val());
                if(v){
                    if(isNaN(v)){
                        $begin.TIP({
                            content: '起点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    }else{
                        v = parseFloat(v);
                        if(v >= 0){
                            return v;
                        }else{
                            $begin.TIP({
                                content: '起点距离须大于或等于0!',
                                zindex: 9
                            });
                            data.mode = false;
                        }
                    }
                }else{
                    $begin.TIP({
                        content: '起点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.endMileage = function () {
                var v1 = $.trim($begin.val());
                var v2 = $.trim($end.val());
                if (v2) {
                    if (isNaN(v2)) {
                        $end.TIP({
                            content: '终点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    } else {
                        v1 = parseFloat(v1);
                        v2 = parseFloat(v2);
                        if(v2 <= v1){
                            $end.TIP({
                                content: '终点距离须大于起点距离!',
                                zindex: 9
                            });
                            data.mode = false;
                        }else{
                            return v2;
                        }
                    }
                } else {
                    $end.TIP({
                        content: '终点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.inspTaskId = function () {
                var v = $inside_task.val();
                if (DyyBase.notEmpty(v)) {
                    return v;
                } else {
                    $inside_task.element.TIP({
                        content: '内检测项目名称不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.exinspTaskStartDate = function () {
                var v = $.trim($start_date.val());
                if (DyyBase.notEmpty(v)) {
                    return v;
                }else{
                    $start_date.TIP({
                        content:'外检起始时间不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.exinspTaskEndDate = function () {
                var v1 = $.trim($start_date.val());
                var v2 = $.trim($end_date.val());
                if (DyyBase.notEmpty(v2)) {
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $end_date.TIP({
                                content:'结束时间不能早于起始时间!',
                                zindex: 9
                            });
                            data.mode = false;
                        }
                    }
                    return v2;
                }else{
                    $end_date.TIP({
                        content:'外检结束时间不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            return data;
        }
    };


    // 1.PCM+ACVG(漏损点分布)
    var pcm_acvg = [
        {x:0,y:0},
        {x:1000,y:10},
        {x:2000,y:20},
        {x:3000,y:30},
        {x:4000,y:40},
        {x:5000,y:50},
        {x:6000,y:60},
        {x:7000,y:70},
        {x:12000,y:80}
    ];

    // 2.CIPS+DCVG(管地电位(mV))
    var cips_dvcg_ground = [
        [
            {x:0,y:0},
            {x:2000,y:120},
            {x:4000,y:100},
            {x:6000,y:160},
            {x:8000,y:120},
            {x:12000,y:180},
            {x:16000,y:100},
            {x:20000,y:200},
            {x:22000,y:120}
        ],
        [
            {x:0,y:200},
            {x:2000,y:320},
            {x:4000,y:300},
            {x:6000,y:360},
            {x:8000,y:320},
            {x:12000,y:380},
            {x:16000,y:300},
            {x:20000,y:400},
            {x:22000,y:320}
        ]
    ];

    // 3.CIPS+DVCG(IR%)
    var cips_dvcg_ir = [
        {x:0,y:0},
        {x:2000,y:120},
        {x:4000,y:100},
        {x:6000,y:160},
        {x:8000,y:120},
        {x:12000,y:180},
        {x:16000,y:100},
        {x:20000,y:200},
        {x:22000,y:120}
    ];

    // 4.测试桩管地电位(mV)
    var test_ground = [
        [
            {x:0,y:0},
            {x:2000,y:120},
            {x:4000,y:100},
            {x:6000,y:160},
            {x:8000,y:120},
            {x:12000,y:180},
            {x:16000,y:100},
            {x:20000,y:200},
            {x:22000,y:120}
        ],
        [
            {x:0,y:200},
            {x:2000,y:320},
            {x:4000,y:300},
            {x:6000,y:360},
            {x:8000,y:320},
            {x:12000,y:380},
            {x:16000,y:300},
            {x:20000,y:400},
            {x:22000,y:320}
        ],
        [
            {x:0,y:400},
            {x:2000,y:520},
            {x:4000,y:500},
            {x:6000,y:560},
            {x:8000,y:520},
            {x:12000,y:580},
            {x:16000,y:500},
            {x:20000,y:600},
            {x:22000,y:520}
        ]
    ];

    // 5.凹陷
    var ao = [
        {x:0,y:'00:00',ys:1},
        {x:1000,y:'01:00',ys:2},
        {x:5000,y:'02:00',ys:3},
        {x:8000,y:'03:00',ys:1},
        {x:10000,y:'04:00',ys:2},
        {x:15000,y:'05:00',ys:3},
        {x:18000,y:'06:00',ys:1},
        {x:19000,y:'07:00',ys:2},
        {x:20000,y:'08:00',ys:3},
        {x:22000,y:'12:00',ys:1}
    ];

    // 6.椭圆变形、带下阴影
    var shadow = [
        {mile:0,type:1},
        {mile:2000,type:2},
        {mile:8000,type:1},
        {mile:10000,type:2},
        {mile:16000,type:1},
        {mile:22000,type:2}
    ];

    // 7.焊缝异常
    var hww = [
        {x:0,y:'00:00',ys:1},
        {x:1000,y:'01:00',ys:2},
        {x:5000,y:'02:00',ys:1},
        {x:8000,y:'03:00',ys:1},
        {x:10000,y:'04:00',ys:2},
        {x:15000,y:'05:00',ys:2},
        {x:18000,y:'06:00',ys:2},
        {x:19000,y:'07:00',ys:1},
        {x:20000,y:'08:00',ys:1},
        {x:22000,y:'12:00',ys:2}
    ];

    // 8.金属损失
    var metal = [
        {x:0,y:'00:00'},
        {x:1000,y:'01:00'},
        {x:5000,y:'02:00'},
        {x:8000,y:'03:00'},
        {x:10000,y:'04:00'},
        {x:15000,y:'05:00'},
        {x:18000,y:'06:00'},
        {x:19000,y:'07:00'},
        {x:20000,y:'08:00'},
        {x:22000,y:'12:00'}
    ];

    // 9.管道基础信息
    var pipeinfo = [
        {mile:0,type:1},
        {mile:2000,type:2},
        {mile:8000,type:1},
        {mile:10000,type:2},
        {mile:16000,type:1},
        {mile:22000,type:2}
    ];


    // 查询
    $('#JQ_search').click(function () {
        var $this = $(this);
        var param = filter.getSearch();
        //param.mode
        if (param.mode) {
            $this.attr('data-search','true');
            var mile = DyyBase.getMinMax(param.startMileage, param.endMileage);
            var min = mile.min, max = mile.max;

            // 1.PCM+ACVG(漏损点分布)
            DyyBase.ajax(analysisApi.url_pcm_acvg(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formart(dataset.result,{x:'mileage2',y:'dbValue'});
                        d3Charts.point({
                            container: 'JQ_pcm_acvg',
                            color: ['#48a2e8'],
                            data: data,
                            areaShow: true,
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: 'dB值'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [],
                                ticks: 5
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            }
                        });
                        $('#JQ_pcm_acvg').trigger('playTag','true');
                    }else{
                        $('#JQ_pcm_acvg').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 2.CIPS+DCVG(管地电位(mV))
            DyyBase.ajax(analysisApi.url_cips_dcvg(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result.cipsOnPotentialList.length && dataset.result.cipsOffPotentialList.length){
                        var on = d3Charts.formart(dataset.result.cipsOnPotentialList,{x:'mileage2',y:'cipsOnPotential'});
                        var off = d3Charts.formart(dataset.result.cipsOffPotentialList,{x:'mileage2',y:'cipsOffPotential'});
                        var all = [on,off];
                        d3Charts.dogLeg({
                            container: 'JQ_cips_dcvg_ground',
                            color: ['#48a2e8','#f09191'],
                            data: all,
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                ticks: Infinity
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            },
                            tip: {
                                show: true,
                                xtag: '绝对距离(m)：',
                                ytag: ['通电电位：','断电电位：']
                            }
                        });
                        $('#JQ_cips_dcvg_ground').trigger('playTag','true');
                    }else{
                        $('#JQ_cips_dcvg_ground').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 3.CIPS+DVCG(IR%)
            DyyBase.ajax(analysisApi.url_cips_dcvg(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result.dcvgIrList && dataset.result.dcvgIrList.length > 0){
                        var ir = d3Charts.formart(dataset.result.dcvgIrList,{x:'mileage2',y:'dcvgIr'});
                        d3Charts.dogLeg({
                            container: 'JQ_cips_dcvg_ir',
                            color: ['#48a2e8'],
                            data: [ir],
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                ticks: Infinity
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            },
                            tip: {
                                show: true,
                                xtag: '绝对距离(m)',
                                ytag: '电位(mV)'
                            }
                        });
                        $('#JQ_cips_dcvg_ir').trigger('playTag','true');
                    }else{
                        $('#JQ_cips_dcvg_ir').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 4.测试桩管地电位(mV)
            DyyBase.ajax(analysisApi.url_test_ground(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var on = d3Charts.formart(dataset.result,{x:'mileage',y:'onPotentialAvg'});
                        var off = d3Charts.formart(dataset.result,{x:'mileage',y:'offPotentialAvg'});
                        var free = d3Charts.formart(dataset.result,{x:'mileage',y:'freeCorrosionPotential'});
                        var all = [on,off,free];
                        d3Charts.dogLeg({
                            container: 'JQ_test_ground',
                            color: ['#48a2e8','#f09191','#6acb81'],
                            data: all,
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                ticks: Infinity
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            },
                            tip: {
                                show: true,
                                xtag: '绝对距离(m)：',
                                ytag: ['通电电位：','断电电位：','自然电位：']
                            }
                        });
                        $('#JQ_test_ground').trigger('playTag','true');
                    }else{
                        $('#JQ_test_ground').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 5.凹陷
            DyyBase.ajax(analysisApi.url_ao_d(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formartTs(dataset.result,{x:'MILEAGE',y:'CLOCK',ys:'TYPE'});
                        d3Charts.pointClock({
                            container: 'JQ_ellipse',
                            color: ['#48a2e8','#f09191','#6acb81'],
                            data: data,
                            areaShow: true,
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: '时钟'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [(new Date(Date.UTC(2016,10,11,0,0))), (new Date(Date.UTC(2016,10,11,12,0)))],
                                ticks: 5,
                                render: function(d){
                                    var h = d.getUTCHours() < 10 ? '0' + d.getUTCHours().toString() : d.getUTCHours().toString();
                                    var m = d.getUTCMinutes() < 10 ? '0' + d.getUTCMinutes().toString() : d.getUTCMinutes().toString();
                                    return h + ':' + m;
                                }
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            }
                        });
                        $('#JQ_ellipse').trigger('playTag','true');
                    }else{
                        $('#JQ_ellipse').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 6.椭圆变形、带下阴影
            DyyBase.ajax(analysisApi.url_ellipse_shadow(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formartYs(dataset.result, {x: 'MILEAGE', y: 'TYPE'});
                        d3Charts.signFx({
                            container: 'JQ_shadow',
                            color: ['#9dcef6', '#f09091'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            pointText: {
                                show: false
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            circleRender: function(d){
                                switch (d.ys){
                                    case 'BELOW_SHADOW':
                                        return '#9dcef6';
                                    break;
                                    case 'ELLIPTIC':
                                        return '#f09091';
                                        break;
                                    default:
                                        return '#454545';
                                        break;
                                }
                            }
                        });
                        $('#JQ_shadow').trigger('playTag','true');
                    } else {
                        $('#JQ_shadow').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 7.焊缝异常
            DyyBase.ajax(analysisApi.url_inside_hww(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formartTs(dataset.result,{x:'MILEAGE',y:'CLOCK',ys:'TYPE'});
                        d3Charts.pointClock({
                            container: 'JQ_hww',
                            color: ['#48a2e8','#f09191'],
                            data: data,
                            areaShow: true,
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: '时钟'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [(new Date(Date.UTC(2016,10,11,0,0))), (new Date(Date.UTC(2016,10,11,12,0)))],
                                ticks: 5,
                                render: function(d){
                                    var h = d.getUTCHours() < 10 ? '0' + d.getUTCHours().toString() : d.getUTCHours().toString();
                                    var m = d.getUTCMinutes() < 10 ? '0' + d.getUTCMinutes().toString() : d.getUTCMinutes().toString();
                                    return h + ':' + m;
                                }
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            },
                            circleRender: function(d){
                                switch (d.ys){
                                    case 'PIPE_WELD':
                                        return '#48a2e8';
                                    break;
                                    case 'GIRTH_WELD':
                                        return '#f09191';
                                        break;
                                    default:
                                        return '#454545';
                                        break;
                                }
                            }
                        });
                        $('#JQ_hww').trigger('playTag','true');
                    }else{
                        $('#JQ_hww').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 8.金属损失
            DyyBase.ajax(analysisApi.url_metal_loss(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formartTs(dataset.result,{x:'MILEAGE',y:'CLOCK'});
                        d3Charts.pointClock({
                            container: 'JQ_metal',
                            color: ['#48a2e8'],
                            data: data,
                            areaShow: true,
                            tip: {
                                xtag: '绝对距离(m)',
                                ytag: '时钟'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [(new Date(Date.UTC(2016,10,11,0,0))), (new Date(Date.UTC(2016,10,11,12,0)))],
                                ticks: 5,
                                render: function(d){
                                    var h = d.getUTCHours() < 10 ? '0' + d.getUTCHours().toString() : d.getUTCHours().toString();
                                    var m = d.getUTCMinutes() < 10 ? '0' + d.getUTCMinutes().toString() : d.getUTCMinutes().toString();
                                    return h + ':' + m;
                                }
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            }
                        });
                        $('#JQ_metal').trigger('playTag','true');
                    }else{
                        $('#JQ_metal').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 9.管道基础信息
            DyyBase.ajax(analysisApi.url_pipe_mark(),param,function(dataset){
                if(dataset.bool){
                    if(dataset.result && dataset.result.length > 0){
                        var data = d3Charts.formartYs(dataset.result,{x:'MILEAGE',y:'TYPE'});
                        d3Charts.signSx({
                            container: 'JQ_pipeinfo',
                            color: ['#f09091', '#7eb9e7'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            pointText: {
                                show: false
                            }
                        });
                        $('#JQ_pipeinfo').trigger('playTag','true');
                    }else{
                        $('#JQ_pipeinfo').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 启用拖拽
            $('#JQ_pdf_area').dragLine();
        }
    });

    // 导出
    $('#JQ_export').click(function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            var param = filter.getSearch();
            var search = $('#JQ_search').attr('data-search');
            if(param.mode){
                if(search === 'true'){
                    $this.EXPT({
                        type: 'name',
                        name: param.pipeLineName + '内外检数据对齐分析图',
                        yesBtnAct: function(callback, fname){
                            callback();
                            canvasPdf.exportHtmlPdf({
                                dom: $('#JQ_pdf_area'),
                                server: DyyBase.root_path + '/comm/downImgPdf.json',
                                param: {
                                    fileName: fname,
                                    name: fname
                                }
                            });
                        }
                    });
                }else{
                    $('#JQ_search').MSG({
                        content: '尚未绘制图形,请先执行查询!'
                    });
                }
            }
        });
    });


})(jQuery, window, document);